package com.xiaocheng.www.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaocheng.www.common.utils.page.Page;
import com.xiaocheng.www.dto.impl.BrandDto;
import com.xiaocheng.www.entiy.impl.Brand;
import com.xiaocheng.www.mapper.MyMapper;
import com.xiaocheng.www.mapper.impl.BrandMapper;
import com.xiaocheng.www.querry.BaseQuerry;
import com.xiaocheng.www.querry.impl.BrandQuerry;
import com.xiaocheng.www.service.BrandService;
import com.xiaocheng.www.service.base.impl.BaseServiceImpl;
import com.xiaocheng.www.transfer.impl.BrandTransfer;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;


@Service

public class BrandServicrImpl extends BaseServiceImpl<Brand> implements BrandService {
    @Autowired
    BrandMapper brandMapper;

    @Autowired
    BrandTransfer brandTransfer;

    public BrandServicrImpl(MyMapper<Brand> myMapper) {
        super(myMapper);
    }


    @Override
    public Page<BrandDto> pageFind(BrandQuerry brandQuerry) {
//        分页开启
        PageHelper.startPage(brandQuerry.getCurrentPage(),brandQuerry.getSize());

        LambdaQueryWrapper<Brand> lambda = new QueryWrapper<Brand>().lambda();

        List<Brand> brands = null;
        if (brandQuerry.isTrue()){
            if (!StringUtils.isEmpty(brandQuerry.getBrandName())){
               lambda.like(Brand::getBrandName, brandQuerry.getBrandName());
            }

            if (!(Objects.isNull(brandQuerry.getStartTime()) && Objects.isNull((brandQuerry.getEndTime())))){
                lambda.between(Brand::getCreateTime, brandQuerry.getStartTime() ,brandQuerry.getEndTime());
            }

         brands = brandMapper.selectList(lambda);

        }else {
            brands = brandMapper.selectList(null);
        }

        PageInfo<Brand> pageInfo = new PageInfo<>(brands);
        long total = pageInfo.getTotal();


        return  new Page<BrandDto>(total,brandTransfer.toDtos(brands));
    }
}
